package com.xy.blog.article.domain.service.impl;

import com.xy.blog.article.api.model.SpecialColumnQuery;
import com.xy.blog.article.domain.dao.ISpecialColumnDao;
import com.xy.blog.article.domain.dao.model.SpecialColumnDO;
import com.xy.blog.article.domain.mapstruct.SpecialColumnConverter;
import com.xy.blog.article.domain.service.ISpecialColumnService;
import com.xy.blog.article.domain.service.model.SpecialColumnBO;
import com.xy.blog.article.domain.service.oss.OSSServices;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import lombok.extern.java.Log;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description: 专栏
 * @date 2020/7/17
 */
@Log
@Service
public class SpecialColumnServiceImpl implements ISpecialColumnService {

    @Autowired
    private ISpecialColumnDao specialColumnDaoImpl;

    @Autowired
    private SpecialColumnConverter specialColumnConverter;

    @Autowired
    private OSSServices ossServices;

    @Override
    public PageResp<SpecialColumnBO> listByPage(PageReq<SpecialColumnQuery> req) throws BaseException {
        PageResp<SpecialColumnDO> specialColumnDOPageResp = specialColumnDaoImpl.listByPage(req);
        PageResp<SpecialColumnBO> specialColumnBOPageResp = specialColumnConverter.do2Bo(specialColumnDOPageResp);
        return specialColumnBOPageResp;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void save(SpecialColumnBO item , byte[] file,String fileName) throws BaseException {
        SpecialColumnDO specialColumnDO = specialColumnConverter.bo2Do(item);
        try {
            StringBuffer filePathTemp = new StringBuffer();
            filePathTemp.append(LocalDate.now().toString()).append("/").append(LocalTime.now().
                    format(DateTimeFormatter.ofPattern("HHmmss"))).append("/").append(fileName);
            fileName = filePathTemp.toString();
            ossServices.saveFile(file, fileName);
        } catch (Exception e) {
            log.log(Level.FINE, ExceptionUtils.getMessage(e));
        }
        specialColumnDO.setImgPath(ossServices.getUrl(fileName));
        specialColumnDO.setViewCount(1L);
        specialColumnDaoImpl.save(specialColumnDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void update(SpecialColumnBO item) throws BaseException {
        SpecialColumnDO specialColumnDO = specialColumnConverter.bo2Do(item);
        specialColumnDaoImpl.updateById(specialColumnDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) throws BaseException {
        specialColumnDaoImpl.deleteById(id);
    }

    @Override
    public SpecialColumnBO getById(Long id) throws BaseException {
        SpecialColumnDO byId = specialColumnDaoImpl.getById(id);
        return specialColumnConverter.do2Bo(byId);
    }

    @Override
    public Object list(String name) throws BaseException {
        SpecialColumnDO specialColumnDO = new SpecialColumnDO();
        specialColumnDO.setName(name);
        return specialColumnDaoImpl.select(specialColumnDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void view(Long id) throws BaseException {
        SpecialColumnDO byId = specialColumnDaoImpl.getById(id);
        byId.setViewCount(byId.getViewCount() + 1);
        specialColumnDaoImpl.updateById(byId);
    }
}
